package com.demo.main.servlet;

import com.demo.main.entity.User;
import com.demo.main.service.*;
import com.demo.main.utils.ReflectionUtil;
import com.demo.main.utils.ServletUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.demo.main.utils.ServletUtil.PERMISSION_PATH_ADMIN;
import static com.demo.main.utils.ServletUtil.PERMISSION_PATH_USER;

@WebServlet({PERMISSION_PATH_USER + "/view/book",
        PERMISSION_PATH_USER + "/view/notice",
        PERMISSION_PATH_USER + "/view/borrow_record",
        PERMISSION_PATH_ADMIN + "/view/book_management",
        PERMISSION_PATH_ADMIN + "/view/book_edit",
        PERMISSION_PATH_ADMIN + "/view/book_add",
        PERMISSION_PATH_ADMIN + "/view/user_management",
        PERMISSION_PATH_ADMIN + "/view/user_edit",
        PERMISSION_PATH_ADMIN + "/view/user_add",
        PERMISSION_PATH_ADMIN + "/view/book_category_management",
        PERMISSION_PATH_ADMIN + "/view/book_category_edit",
        PERMISSION_PATH_ADMIN + "/view/book_category_add",
        PERMISSION_PATH_ADMIN + "/view/notice_management",
        PERMISSION_PATH_ADMIN + "/view/notice_edit",
        PERMISSION_PATH_ADMIN + "/view/notice_add",
        PERMISSION_PATH_ADMIN + "/view/borrow_record",
        PERMISSION_PATH_ADMIN + "/view/data_analysis",
})
public class RouterServlet extends HttpServlet {

    private final UserService userService = new UserService();
    private final BookService bookService = new BookService();
    private final NoticeService noticeService = new NoticeService();
    private final BookCategoryService bookCategoryService = new BookCategoryService();
    private final BorrowRecordService borrowRecordService = new BorrowRecordService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        ServletUtil servletUtil = new ServletUtil(request, response);
        switch (request.getServletPath()) {
            case PERMISSION_PATH_USER + "/view/book":
                servletUtil.setAttributesIfAbsent(
                        "list", bookService.findBookVo(),
                        "pageCount", bookService.findBookVoPageCountByCondition(),
                        "categoryList", bookCategoryService.findAll());
                servletUtil.forwardToView("user/book");
                break;
            case PERMISSION_PATH_USER + "/view/notice":
                request.setAttribute("list", noticeService.findAllVoDesc());
                servletUtil.forwardToView("user/notice");
                break;
            case PERMISSION_PATH_USER + "/view/borrow_record":
                servletUtil.setAttributesIfAbsent(
                        "list", borrowRecordService.findBorrowRecordVoByCurrentUser(servletUtil.getSessionUserId(int.class), 1),
                        "pageCount", borrowRecordService.findBorrowRecordCountByCondition(servletUtil.getSessionUserId(int.class)));
                servletUtil.forwardToView("user/borrow_record");
                break;

            case PERMISSION_PATH_ADMIN + "/view/book_management":
                servletUtil.setAttributesIfAbsent(
                        "list", bookService.findBookVo(),
                        "pageCount", bookService.findBookVoPageCountByCondition(),
                        "categoryList", bookCategoryService.findAll());
                servletUtil.forwardToView("admin/book_management");
                break;

            case PERMISSION_PATH_ADMIN + "/view/book_edit":
                Integer bookId = servletUtil.getParameterIntegerId();
                servletUtil.setAttributesIfAbsent(
                        "categoryList", bookCategoryService.findAll(),
                        "entity", bookService.findOneById(bookId),
                        "isAdd", false);
                servletUtil.forwardToView("admin/book_add");
                break;

            case PERMISSION_PATH_ADMIN + "/view/book_add":
                servletUtil.setAttributesIfAbsent("categoryList", bookCategoryService.findAll(),
                        "isAdd", true);
                servletUtil.forwardToView("admin/book_add");
                break;
            case PERMISSION_PATH_ADMIN + "/view/user_management":
                User cUser = ReflectionUtil.createAndSetField("role", "user", User.class);
                servletUtil.setAttributesIfAbsent(
                        "list", userService.findSelectivePagination(cUser, 1),
                        "pageCount", userService.findPageCountByCondition(cUser));
                servletUtil.forwardToView("admin/user_management");
                break;
            case PERMISSION_PATH_ADMIN + "/view/user_edit":
                servletUtil.setAttributesIfAbsent(
                        "entity", userService.findOneById(servletUtil.getParameterIntegerId()),
                        "isAdd", false);
                servletUtil.forwardToView("admin/user_add");
                break;
            case PERMISSION_PATH_ADMIN + "/view/user_add":
                servletUtil.setAttributesIfAbsent("isAdd", true);
                servletUtil.forwardToView("admin/user_add");
                break;
            case PERMISSION_PATH_ADMIN + "/view/book_category_management":
                servletUtil.setAttributesIfAbsent("list", bookCategoryService.findAllByDesc());
                servletUtil.forwardToView("admin/book_category_management");
                break;
            case PERMISSION_PATH_ADMIN + "/view/book_category_edit":
                servletUtil.setAttributesIfAbsent(
                        "isAdd", false,
                        "entity", bookCategoryService.findOneById(servletUtil.getParameterIntegerId()));
                servletUtil.forwardToView("admin/book_category_add");
                break;
            case PERMISSION_PATH_ADMIN + "/view/book_category_add":
                servletUtil.setAttributesIfAbsent("isAdd", true);
                servletUtil.forwardToView("admin/book_category_add");
                break;
            case PERMISSION_PATH_ADMIN + "/view/notice_management":
                servletUtil.setAttributesIfAbsent("list", noticeService.findAllVoDesc());
                servletUtil.forwardToView("admin/notice_management");
                break;

            // 当请求路径为 "/view/notice_add" 时
            case PERMISSION_PATH_ADMIN + "/view/notice_add":
                // 设置属性 "isAdd" 为 true，如果该属性不存在
                servletUtil.setAttributesIfAbsent("isAdd", true);
                // 转发到视图 "admin/notice_add"
                servletUtil.forwardToView("admin/notice_add");
                break;

            // 当请求路径为 "/view/notice_edit" 时
            case PERMISSION_PATH_ADMIN + "/view/notice_edit":
                // 设置属性 "isAdd" 为 false，如果该属性不存在
                // 同时设置属性 "entity" 为通过 ID 查找到的通知实体对象
                servletUtil.setAttributesIfAbsent(
                        "isAdd", false,
                        "entity", noticeService.findOneById(servletUtil.getParameterIntegerId()));
                // 转发到视图 "admin/notice_add"
                servletUtil.forwardToView("admin/notice_add");
                break;

            // 当请求路径为 "/view/borrow_record" 时
            case PERMISSION_PATH_ADMIN + "/view/borrow_record":
                // 设置属性 "list" 为所有借阅记录的视图对象列表，如果该属性不存在
                // 同时设置属性 "pageCount" 为借阅记录的页数，如果该属性不存在
                servletUtil.setAttributesIfAbsent(
                        "list", borrowRecordService.findAllBorrowRecordVo(),
                        "pageCount", borrowRecordService.findPageCount());
                // 转发到视图 "admin/borrow_record"
                servletUtil.forwardToView("admin/borrow_record");
                break;

            // 当请求路径为 "/view/data_analysis" 时
            case PERMISSION_PATH_ADMIN + "/view/data_analysis":
                // 设置属性 "list" 为所有借阅记录的视图对象列表，如果该属性不存在
                // 同时设置属性 "pageCount" 为借阅记录的页数，如果该属性不存在
                servletUtil.setAttributesIfAbsent(
                        "list", borrowRecordService.findAllBorrowRecordVo(),
                        "pageCount", borrowRecordService.findPageCount());
                // 转发到视图 "admin/borrow_record"
                servletUtil.forwardToView("admin/data_analysis");
                break;
        }
    }
}
